package com.gxgeek.springdata.domain.jpa.repository;

import com.gxgeek.springdata.domain.jpa.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;


/**
 * Created by gx on 2017/4/21.
 */
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {

    Employee findByName(String name);
    // 以什么为前缀  并且年龄小于 X
    List<Employee> findByNameStartingWithAndAgeLessThan(String name,Integer age);
    // 以什么为结束  并且年龄小于 X
    List<Employee> findByNameEndingWithAndAgeLessThan(String name,Integer age);
    //姓名在某个区域 或年龄 小于
    List<Employee> findByNameInOrAgeLessThan(List<String> name,Integer age);
    //姓名在某个区域 并且年龄 小于
    List<Employee> findByNameInAndAgeLessThan(List<String> name,Integer age);


    @Query("SELECT o from Employee o where  id=(select max (id) from  Employee t1)")
    Employee getEmployeeByMaxId();

    @Query("SELECT o from Employee o WHERE  o.name =?1 and  o.age=?2 ")
    Employee getEmployeeByNameAndAge(String name,Integer age);


    @Query("SELECT o from Employee o WHERE  o.name =:name and  o.age=:age ")
    Employee getParams(@Param("name") String name, @Param("age")Integer age);


    @Query("SELECT o from Employee o WHERE  o.name  like %?1%  ")
    List<Employee> queryByLike(String name);

    @Query("SELECT o from Employee o WHERE  o.name  like %:name%  ")
    List<Employee> queryByLike2(@Param("name") String name);

    //使用原生
    @Query(nativeQuery = true,value = "select count(1) from employee  ")
    Integer queryByative();

    @Modifying
    @Query(" update  Employee o set  o.age= :age where  o.name= :name")
    Integer update(@Param("name") String name, @Param("age")Integer age);

    @Modifying
    @Query("update Employee o set o.age = :age where o.id = :id")
    public void update2(@Param("id")Integer id, @Param("age")Integer age);
}
